//
//  YHPVisitorLoginView.swift
//  SinaSwift
//
//  Created by Yan on 16/3/6.
//  Copyright © 2016年 baidu. All rights reserved.
//

import UIKit
import SnapKit

//MARK: 代理协议
@objc protocol YHPVisitorLoginViewDelegate : NSObjectProtocol {
    // 代理方法
    optional func userWillLogin()
    func userWillRegistor ()
}

class YHPVisitorLoginView: UIView {
    
    //MARK: 代理属性 weak修饰
    weak var visitorLoginViewDelegate : YHPVisitorLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        
    }
    
    // 修改view信息,原来是circleIcon largeHouse tipLabel 其他页面没有circleIcon 和largeHouse 在此替换circleIcon 和 tipLabel 并隐藏largeHouse 不替换circleIcon即传入的circleIcon为nil
    func setUpInfoWith(title : String,imageName : String?) {
        
        tipLabel.text = title
        if imageName != nil {
            // 是不是首页
            circleIcon.image = UIImage(named: imageName!)
            largeHouse.hidden = true
            maskImage.hidden = true
            //设置不是渐隐
            //bringSubviewToFront(circleIcon)
        } else {
            // 是首页
              startAnimation()          
        }
    }

    required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var circleIcon : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var largeHouse : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var maskImage : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var loginBtn : UIButton = UIFactory.factoryButtonWithTitle("登录", fontSize: 14, titleColor: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
    private lazy var registBtn : UIButton = UIFactory.factoryButtonWithTitle("注册", fontSize: 14, titleColor: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
    
    func userLogin () {
      visitorLoginViewDelegate?.userWillLogin!()
    }
    
    func userRegister () {
        visitorLoginViewDelegate?.userWillRegistor()
    }
    private lazy var tipLabel: UILabel = UIFactory.factoryLabelWith("轻松注册,分享快乐,传递幸福,迎娶白富美,出任CEO,走向人生巅峰.", textColor: UIColor.darkGrayColor(), fontSize: 16)
    func setUpUI () {
        
        // 取色器获取值236 设置view背景色
        let value : CGFloat = 236.0 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        addSubview(circleIcon)
        //circleIcon相对于view约束 centerX  centerY-100
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-100)
        }
        addSubview(maskImage)
        maskImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.bottom.equalTo(circleIcon.snp_bottom)
        }
        addSubview(largeHouse)
        //largeHouse相对于circleIcon约束 centerX centerY-6
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.centerY.equalTo(circleIcon.snp_centerY).offset(-6)
            //make.center.equalTo(circleIcon.snp_center)
        }
        addSubview(tipLabel)
        //tipLabel相对于circleIcon约束centerX bottom+15
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.centerY.equalTo(circleIcon.snp_bottom).offset(15)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        addSubview(loginBtn)
        //registBtn相对于tipLabel约束左对齐 bottom+18
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.centerY.equalTo(tipLabel.snp_bottom).offset(28)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        addSubview(registBtn)
        //loginBtn相对于tipLabel约束右对齐 bottom+18
        registBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.centerY.equalTo(tipLabel.snp_bottom).offset(28)
            make.width.equalTo(100)
            make.height.equalTo(36)
        registBtn.addTarget(self, action: "userRegister", forControlEvents: UIControlEvents.TouchUpInside)
        loginBtn.addTarget(self, action: "userLogin", forControlEvents: UIControlEvents.TouchUpInside)
        }
    }
    
    
    // 设置circleIcon动画 旋转
    func startAnimation () {
        // 定义基本动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 动画时间10秒
        animation.duration = 10
        // 动画重复次数无限大
        animation.repeatCount = MAXFLOAT
        // 动画旋转多少 一圈
        animation.toValue = 2 * M_PI
        //在动画结束 或者页面处于非活跃状态时 动画效果不移除图层
        animation.removedOnCompletion = false
        // 给circleIcon添加动画
        circleIcon.layer.addAnimation(animation, forKey: nil)
    }
    
 
   
 
}
